package com.xyz.controller;

import com.xyz.aop.Param;
import com.xyz.service.RoleService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("system")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @PostMapping("selectUserInfo")
    public DataResult selectUserInfo(
            LoginInfo li
    ) {
        try {
            return roleService.selectUserInfo(li);
        } catch (Exception e) {
            logger.error("selectUserInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectRole")
    public DataResult selectRole(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String roleName
    ) {
        try {
            return roleService.selectRole(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), roleName);
        } catch (Exception e) {
            logger.error("selectRole", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertRole")
    public DataResult insertRole(
            LoginInfo li,
            @Param String roleName
    ) {
        try {
            return roleService.insertRole(li, roleName);
        } catch (Exception e) {
            logger.error("insertRole", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateRole")
    public DataResult updateRole(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId,
            @Param String roleName
    ) {
        try {
            return roleService.updateRole(li, Long.parseLong(roleId), roleName);
        } catch (Exception e) {
            logger.error("updateRole", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteRole")
    public DataResult deleteRole(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId
    ) {
        try {
            return roleService.deleteRole(li, Long.parseLong(roleId));
        } catch (Exception e) {
            logger.error("deleteRole", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectDirectoryByRoleId")
    public DataResult selectDirectoryByRoleId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId
    ) {
        try {
            return roleService.selectDirectoryByRoleId(li, Long.parseLong(roleId));
        } catch (Exception e) {
            logger.error("selectDirectoryByRoleId", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateDirectoryByRoleId")
    public DataResult updateDirectoryByRoleId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId,
            @Param(isRequired = false) String directoryIds
    ) {
        try {
            return roleService.updateDirectoryByRoleId(li, Long.parseLong(roleId), directoryIds);
        } catch (Exception e) {
            logger.error("updateDirectoryByRoleId", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectUserByRoleId")
    public DataResult selectUserByRoleId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId
    ) {
        try {
            return roleService.selectUserByRoleId(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(roleId));
        } catch (Exception e) {
            logger.error("selectUserByRoleId", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertUserByRoleId")
    public DataResult insertUserByRoleId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String roleId,
            @Param String userIds
    ) {
        try {
            return roleService.insertUserByRoleId(li, Long.parseLong(roleId), userIds);
        } catch (Exception e) {
            logger.error("insertUserByRoleId", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteUserByRoleId")
    public DataResult deleteUserByRoleId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String userId
    ) {
        try {
            return roleService.deleteUserByRoleId(li, Long.parseLong(userId));
        } catch (Exception e) {
            logger.error("insertUserByRoleId", e);
            return DataResult.build9500();
        }
    }

}
